{% extends 'iframe/basePlus.html.twig' %}
{% block search %}
    {% if searchCode.html %}
    <style>
        .search-item {
            text-align: right;
            line-height: 38px;
        }
    </style>
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-header">搜索</div>
            <div class="layui-card-body">
                <form class="layui-form" onsubmit="return false;" id="searchForm">
                    <div class="layui-row">
                        {{ searchCode.html|raw }}
                        <div class="layui-col-md12" style="text-align:center;padding-top:10px;">
                            <button type="submit" class="layui-btn" lay-submit="" lay-filter="default">确定</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    {% endif %}
{% endblock %}
{% block cardHeader %}
    列表
    <div class="layui-btn layui-hide"></div>
    {% for button in buttonList.cardRightTop.button %}
        {{ button|raw }}
    {% endfor %}
{% endblock %}
{% block cardBody %}
    <style>
        .layui-table-cell {
            height: auto;
        }
    </style>
    <script type="text/html" id="toolbar">
        <div class="layui-btn-container">
            {% for button in buttonList.tableLeftTop.button %}
                {{ button|raw }}
            {% endfor %}
        </div>
    </script>
    <script type="text/html" id="bar">
        {% for button in buttonList.dataRow.button %}
            {{ button|raw }}
        {% endfor %}
    </script>
    <table class="layui-hide" id="list" lay-filter="list"></table>
    <script>
        function dryCommonReady(t)
        {
            toggleInput(t);
        }

        function dryCommonSelectChange(t)
        {
            var name = t.elem.getAttribute("name");
            var tag = document.querySelector(`dry-common[name='${name}']`);
            toggleInput(tag);
        }

        function toggleInput(dryCommonTag)
        {
            if(dryCommonTag.getAttribute("flag") == "sql_operate"){
                var fieldName = dryCommonTag.getAttribute("data-field-name");
                var value = $$(`${fieldName}_sql_operate`).value;
                var input1 = $$(`${fieldName}_value_0`);
                var input2 = $$(`${fieldName}_value_1`);
                if(contain("is_null,is_not_null,is_empty,is_not_empty", value)){
                    input1.style.display = "none";
                    input2.style.display = "none";
                }
                else if(contain("between_and,not_between_and", value)){
                    input1.style.display = "inline-block";
                    input2.style.display = "inline-block";
                }
                else{
                    input1.style.display = "inline-block";
                    input2.style.display = "none";
                }
            }
        }

        function getIndexColumn()
        {
            var list = {{ html|raw }};
            return list;
        }

        layui.use(['dry', 'file', 'jquery'], function(dry, fileModule, $){
            var url = {
                "newUrl": "/Admin/Automate/new{{ alias }}",
                "showUrl": "/Admin/Automate/show{{ alias }}",
                "editUrl": "/Admin/Automate/edit{{ alias }}"
            };
            var isIframe = {{ isIframe }};
            var f = {};

            /*即时编辑*/
            f.immediatelyEdit = function(object)
            {
                var parameter = {
                    "id": object.data.id,
                    "field": object.field,
                    "value": object.value
                };
                $.post("/Api/Automate/editPlus{{ alias }}", parameter);
            };

            /*表格渲染结束*/
            f.done = function(response, currentPage, count)
            {
                dry.mergeRequest();
                dry.htmlShow();
                dry.codeShow();
                fileModule.doPlay();
            };

            /*按钮处理函数 start*/
            {% for javascript in buttonList.cardRightTop.javascript %}
                {{ javascript|raw }}
            {% endfor %}
            {% for javascript in buttonList.tableLeftTop.javascript %}
                {{ javascript|raw }}
            {% endfor %}
            {% for javascript in buttonList.dataRow.javascript %}
                {{ javascript|raw }}
            {% endfor %}
            dry.cardRightTopButton(f);
            /*按钮处理函数 end*/

            /*搜索*/
            f.search = function()
            {
                function getSearchData(json)
                {
                    function inner(type, item)
                    {
                        var field = item.field;
                        var ids = item.id;
                        var valueList = [];
                        valueList.push(type);
                        for(var i in ids){
                            valueList.push($("#" + ids[i]).val());
                        }
                        kv[field] = valueList.join("|*|");
                    }

                    var input = json.input;
                    var select = json.select;
                    var kv = {};

                    for(var i in input){
                        inner("input", input[i]);
                    }
                    for(var i in select){
                        inner("select", select[i]);
                    }
                    return kv;
                }

                var json = JSON.parse('{{ searchCode.json|raw }}');
                return getSearchData(json);
            };

            function isReady()
            {
                var length = $("#searchForm dry-common").length;
                var ok = 0;
                $("#searchForm dry-common").each(function(){
                    if($(this).attr("ok") == "1"){
                        ok++;
                    }
                });
                if(ok == length){
                    return true;
                }
                return false;
            }

            function list()
            {
                var result = dry.list("/Api/Automate/index{{ alias }}", getIndexColumn(), {{ limit }}, f);
                config = result.config;
                instance = result.instance;
            }

            var config = null;
            var instance = null;

            dry.runAfter(isReady, list);

            $("button[type='submit']").click(function(){
                config.page = {"curr": 1};
                config.where = f['search']();
                instance.reload(config);
            });
        });
    </script>
{% endblock %}